package com.lh.controller;


import com.lh.commonutils.ordervo.UcenterMemberOrder;
import com.lh.service.IUcenterMemberService;
import com.lh.servicebase.utils.JwtUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import com.lh.servicebase.exceptionhandler.MyException;
import com.lh.commonutils.R;
import com.lh.entity.UcenterMember;
import com.lh.entity.vo.RegisterVo;
import io.swagger.annotations.ApiOperation;


import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 会员表 前端控制器
 * </p>
 *
 * @author lh
 * @since 2021-06-09
 */
@RestController
@RequestMapping("/eduucenter/member")
public class UcenterMemberController {

    @Resource
    private IUcenterMemberService ucenterMemberService;



    @GetMapping("countRegister/{day}")
    public Integer countRegister(@PathVariable String day) {

       Integer count= ucenterMemberService.countRegister(day);
       return count;

    }


    @ApiOperation(value = "会员登录")
    @PostMapping("login")
    public R loginUser( @RequestBody UcenterMember member){

         String token =  ucenterMemberService.login(member);

         return R.ok().data("token",token);
    }


    @ApiOperation("注册")
    @PostMapping("register")
    public  R registerUser(@RequestBody RegisterVo registerVo){
        ucenterMemberService.register(registerVo);
        return R.ok();
    }


    @ApiOperation(value = "根据token获取登录信息")
    @GetMapping("getMemberInfo")
    public R getLoginInfo(HttpServletRequest request){
        try {
            String memberId = JwtUtils.getMemberIdByJwtToken(request);
            UcenterMember member = ucenterMemberService.getById(memberId);
            return R.ok().data("item", member);
        }catch (Exception e){
            e.printStackTrace();
            throw new MyException(20001,"error");
        }
    }
    @PostMapping("getInfoUc/{id}")
    @ApiOperation("根据token字符串获取用户信息")
    public UcenterMember getInfo(@PathVariable String id) {
        //根据用户id获取用户信息
        UcenterMember ucenterMember = ucenterMemberService.getById(id);
        UcenterMember memeber = new UcenterMember();
        BeanUtils.copyProperties(ucenterMember,memeber);
        return memeber;
    }


    @ApiOperation("根据id获取个人信息")
    @PostMapping("getUserInfoOrder/{id}")
    public UcenterMemberOrder getUserInfoOrser(@PathVariable String id){
        UcenterMember member = ucenterMemberService.getById(id);
        UcenterMemberOrder ucenterMemberOrder = new UcenterMemberOrder();
        BeanUtils.copyProperties(member,ucenterMemberOrder);

        return ucenterMemberOrder;
    }


    @ApiOperation(value = "将用户身份改变为讲师身份")
    @PostMapping("changeTeacher/{id}")
    public void changeTeacher(@PathVariable String id){
        UcenterMember member = new UcenterMember();
        member.setId(id);
        member.setIdentity(1);
        ucenterMemberService.updateById(member);
    }

    @ApiOperation("修改用户信息")
    @PostMapping("updateUcenter")
    public R updateUcenter(@RequestBody UcenterMember ucenterMember){
        ucenterMemberService.updateUcenter(ucenterMember);
        return R.ok();
    }


    @ApiOperation("将老师变为普通用户")
    @GetMapping("changeUcenter/{id}")
    public void changeUcenter(@PathVariable String id ){
        UcenterMember member = new UcenterMember();
        member.setId(id);
        member.setIdentity(0);
        ucenterMemberService.updateById(member);

    }


}

